<script setup>
import {emailVerifyCodeGetAPI, recoverPasswordAPI} from "@/api/FrontApi/user.js";
import {ref} from "vue";
import 'element-plus/theme-chalk/el-message.css'
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

const recoverPasswordData = ref({
  email: null,
  code: null,
  password: null,
  rePassword: null
})

// 发送邮箱验证码
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const sendEmailVerityCode = async () => {
  // 检查邮箱是否存在且格式正确
  if (!recoverPasswordData.value.email) {
    ElMessage.error("请输入邮箱地址");
    return;
  }
  if (!emailRegex.test(recoverPasswordData.value.email)) {
    ElMessage.error("请输入有效的邮箱地址");
    return;
  }
  const res = await emailVerifyCodeGetAPI(recoverPasswordData.value.email)
  if (res.code === 1) {
    ElMessage.success("已发送验证码，请注意查收")
  }
}

// 表单校验函数
const validateRecoverPasswordForm = () => {
  let valid = true;
  // 校验邮箱
  if (!recoverPasswordData.value.email) {
    ElMessage.warning('请输入邮箱');
    valid = false;
    return valid;
  }
  // 校验验证码
  if (!recoverPasswordData.value.code) {
    ElMessage.warning('请输入验证码');
    valid = false;
    return valid;
  }
  // 校验密码
  if (!recoverPasswordData.value.password || recoverPasswordData.value.password.length < 6 || !/^[A-Za-z0-9]+$/.test(recoverPasswordData.value.password)) {
    ElMessage.warning('密码不得低于6位，且由字母和数字组成');
    valid = false;
    return valid;
  }
  // 校验确认密码
  if (recoverPasswordData.value.rePassword !== recoverPasswordData.value.password) {
    ElMessage.warning('两次密码输入不一致！');
    valid = false;
    return valid;
  }
  // 如果所有校验都通过，则valid保持为true
  return valid;
}

// 注册处理
const router = useRouter()
const handleSubmit = async () => {
  if (!validateRecoverPasswordForm()) {
    return
  }
  const res = await recoverPasswordAPI(recoverPasswordData.value)
  if (res.code === 1) {
    ElMessage.success("修改成功")
    router.push('/login')
  }
}
</script>

<template>
  <div class="forget-password-page">
    <div class="container">
      <div class="fgtpwd-content">
        <div class="card">
          <h1 class="title">找回密码</h1>
          <div class="form-card">
            <div class="fgtpwd-desc">用邮箱找回</div>
            <div class="fgtpwd-form">
              <form @submit.prevent="handleSubmit">
                <div class="email">
                  <div class="desc">邮箱</div>
                  <input v-model="recoverPasswordData.email" class="email-input">
                </div>
                <div class="verity-code">
                  <div class="desc">验证码</div>
                  <div class="verity-code-container">
                    <input v-model="recoverPasswordData.code" type="password" class="verity-code-input">
                    <div class="send-email-code" @click="sendEmailVerityCode">发送邮箱验证码</div>
                  </div>
                </div>
                <div class="password">
                  <div class="desc">新密码</div>
                  <input v-model="recoverPasswordData.password" type="password" class="password-input">
                </div>
                <div class="password">
                  <div class="desc">确认密码</div>
                  <input v-model="recoverPasswordData.rePassword" type="password" class="re-password-input">
                </div>
                <div class="recover-submit">
                  <div class="desc"></div>
                  <button type="submit" class="recover-submit-button">提交</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.forget-password-page {
  .container {
    width: 100%;
    .fgtpwd-content {
      height: 820px;
      background: radial-gradient(rgba(24, 26, 33, 0) 0%, #181A21 100%) fixed no-repeat, url(/src/assets/images/LoginBGC.jpg) center top no-repeat, #181A21;
      padding-top: 80px;
      padding-bottom: 150px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .card {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        gap: 32px;
        .title {
          color: #fff;
          display: flex;
          flex-direction: column;
          margin: 8px 16px;
        }
        .form-card {
          min-width: 400px;
          min-height: 300px;
          background-color: #181a21;
          border-radius: 4px;
          padding: 24px 96px;
          .fgtpwd-desc {
            color: #1999ff;
            font-size: 18px;
            margin-bottom: 16px;
          }
          .fgtpwd-form {
            width: 400px;
            color: #afafaf;
            font-size: 12px;
            div {
              margin-bottom: 10px;
              div {
                margin-bottom: 0;
              }
              input {
                margin-top: 2px;
                padding: 10px;
                width: 400px;
                height: 40px;
                outline: none;
                font-size: 15px;
              }
            }
            .verity-code {
              margin-bottom: 10px;
              .verity-code-container {
                display: flex;
                justify-content: space-between;
                .verity-code-input {
                  margin-top: 2px;
                  padding: 10px;
                  width: 180px;
                  height: 40px;
                  outline: none;
                  font-size: 15px;
                }
                .send-email-code {
                  margin-top: 2px;
                  width: 160px;
                  height: 40px;
                  line-height: 40px;
                  text-align: center;
                  background: linear-gradient(90deg, #ffa4f6 0%, $secondaryColor 100%);
                  border-radius: 2px;
                  color: #fff;
                  font-size: 14px;
                  cursor: pointer;
                  &:hover {
                    background: linear-gradient(90deg, $secondaryColor 0%, #ff2bc0 100%);
                  }
                }
              }
            }
            .recover-submit {
              margin-top: 30px;
              margin-bottom: 10px;
              display: flex;
              flex-direction: column;
              align-items: center;
              .recover-submit-button {
                width: 270px;
                background: linear-gradient(90deg, #06BFFF 0%, #2D73FF 100%);
                border-radius: 2px;
                border: none;
                outline: none;
                padding: 12px;
                color: #fff;
                font-size: 16px;
                font-weight: 400;
                text-align: center;
                cursor: pointer;
              }
            }
          }
        }
      }
    }
  }
}
</style>
